<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Http\Requests\CategoryRequest;

class CategoryController extends CommonController
{
    /**
     * 获取行业分类列表
     *
     */
    public function getIndex()
    {
        $lists = Category::with('pCategory')->orderBy('id', 'asc')->paginate(10);

//        dd($lists[1]->pCategory->name);
        return view('category.index')->with("lists", $lists)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = Category::with('pCategory')->orderBy('id', 'asc');

        if (isset($data['name']) && $data['name'] != '') {
            $sql->where('name', 'like', '%' . trim($data['name']) . '%');
        }

        if (isset($data['recommend']) && $data['recommend'] != '') {
            $sql->where('recommend', $data['recommend']);
        }

        if (isset($data['status']) && $data['status'] != '') {
            $sql->where('status', $data['status']);
        }

        if (isset($data['hot']) && $data['hot'] != '') {
            $sql->where('hot', $data['hot']);
        }

        $lists = $sql->paginate(10);

        return view('category.search')->with("lists", $lists)->with("name", $data['name'])
            ->with("recommend", $data['recommend'])->with("status", $data['status'])
            ->with("hot", $data['hot'])->with("actions", $this->actions);

    }

    /**
     * 获取创建一个分类的页面
     */
    public function getCreate()
    {
        $lists = Category::with('pCategory')->orderBy('id', 'asc')->get();

        return view('category.create')->with("lists", $lists);
    }

    /**
     * 执行一个行业分类的创建
     */
    public function postCreate(CategoryRequest $request)
    {
        $data = $request->input();
//        dd($data);
        unset($data['_token'], $data['formType']);
        $result = Category::create($data);
        //更新缓存
        Category::cache(1);

        return AjaxCallbackMessage('恭喜,创建成功，新分类的id为' . $result->id, true, url('category/index'));
    }

    /**
     * 获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $lists = Category::with('pCategory')->orderBy('id', 'asc')->get();
        $me = Category::where('id', $data['id'])->first();

        //获取该分类的子孙树
        $family = subTree($lists, $data['id']);

        $others = array();
        foreach ($lists as $k => $v) {
            if (!in_array($v, $family) && $v->id != $me->id) {
                $others[] = $v;
            }
        }

//        dd($family);

        return view('category.edit')->with('others', $others)->with('me', $me);
    }

    /**
     * 执行一个行业编辑
     */
    public function postEdit(CategoryRequest $request)
    {
        $data = $request->input();
        $id = $data['id'];
        unset($data['id'], $data['_token'], $data['formType']);
        Category::where('id', $id)->update($data);
        //更新缓存
        Category::cache(1);

        return AjaxCallbackMessage('恭喜，修改成功', true, url('category/index'));
    }

    public function postDelete(Request $request)
    {
        $id = $request->input('cid');
        Category::where('id', $id)->delete();
        //更新缓存
        Category::cache(1);

        return AjaxCallbackMessage('恭喜，删除成功', true, url('category/index'));
    }
}
